package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: DishServiceImpl
 * Package: IntelliJ IDEA
 *
 * @author 张文豪
 * @description
 * @date 2023/8/16 13:03
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            for(DishFlavor dishFlavor : flavors){
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public DishDTO selectId(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish,dishDTO);
        QueryWrapper<DishFlavor> qw = new QueryWrapper<>();
        qw.lambda().eq(DishFlavor::getDishId,id);
        dishDTO.setFlavors(dishFlavorMapper.selectList(qw));
        return dishDTO;
    }

    @Override
    public List<Dish> selectCategory(Long categoryId) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> dishes = dishMapper.selectList(lambdaQueryWrapper);
        return dishes;
    }

    @Override
    public PageResult pageQueryDish(DishPageQueryDTO dishPageQueryDTO) {
        Page<Dish> myPage = new Page<>(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize(),true);

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dishPageQueryDTO.getCategoryId() != null,Dish::getCategoryId,
                dishPageQueryDTO.getCategoryId()).eq(dishPageQueryDTO.getStatus() != null,Dish
        ::getStatus,dishPageQueryDTO.getStatus());


        lambdaQueryWrapper.like(dishPageQueryDTO.getName() != null,Dish::getName,dishPageQueryDTO.getName());

        IPage<Dish> iPage = dishMapper.selectPage(myPage,lambdaQueryWrapper);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(iPage.getTotal());
        pageResult.setRecords(iPage.getRecords());
        return pageResult;
    }

    @Override
    public void isSell(int status,long id) {
        LambdaUpdateWrapper<Dish> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Dish::getId,id).set(Dish::getStatus,status);
        dishMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //删除之前的口味
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DishFlavor::getDishId,dishDTO.getId());
        dishFlavorMapper.delete(wrapper);
        //添加传过来的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            for(DishFlavor dishFlavor : flavors){
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }

        dishMapper.updateById(dish);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                //当前菜品是否起售
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getDishId,id);
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
            if(setmealDishes != null && setmealDishes.size() != 0){
                //当前菜品是否关联了套餐
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //删除菜品关联的口味
            QueryWrapper<DishFlavor> qw = new QueryWrapper<>();
            qw.lambda().eq(DishFlavor::getDishId,id);
            dishFlavorMapper.delete(qw);
        }
        //删除菜品
        dishMapper.deleteBatchIds(ids);
    }

    @Override
    public List<DishVO> selectById(Long categoryId) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,categoryId).eq(Dish::getStatus,StatusConstant.ENABLE);
        List<Dish> list = dishMapper.selectList(lambdaQueryWrapper);
        List<DishVO> dishVOS = new ArrayList<>();

        for (Dish dish : list) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            //将口味数据传给vo
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
            dishVO.setFlavors(dishFlavors);
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }

    @Override
    public List<DishItemVO> selectBySetmeal(Long id) {
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        return null;

    }

    @Override
    public List<DishItemVO> selectByIds(List<Long> list) {
        List<DishItemVO> list1 = new ArrayList<>();
        for (Long aLong : list) {
            Dish dish = dishMapper.selectById(aLong);
            DishItemVO dishItemVO = new DishItemVO();
            BeanUtils.copyProperties(dish,dishItemVO);
            list1.add(dishItemVO);
        }
        return list1;
    }
}
